package sdk.webview.fmc.com.fmcsdk.h5;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.king.app.updater.AppUpdater;

import java.io.File;
import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import sdk.webview.fmc.com.fmcsdk.BuildConfig;
import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.activity.H5Activity;
import sdk.webview.fmc.com.fmcsdk.app.AppManager;
import sdk.webview.fmc.com.fmcsdk.app.FMCApplication;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.CommonApi;
import sdk.webview.fmc.com.fmcsdk.base.UserApi;
import sdk.webview.fmc.com.fmcsdk.bean.DDTypeBean;
import sdk.webview.fmc.com.fmcsdk.bean.DailyLoginTime;
import sdk.webview.fmc.com.fmcsdk.bean.JpushAliasBean;
import sdk.webview.fmc.com.fmcsdk.bean.ModfiyPassword;
import sdk.webview.fmc.com.fmcsdk.bean.NewVervion;
import sdk.webview.fmc.com.fmcsdk.bean.OperateReportBean;
import sdk.webview.fmc.com.fmcsdk.bean.PhotoBase64Bean;
import sdk.webview.fmc.com.fmcsdk.bean.PhotoUploadBean;
import sdk.webview.fmc.com.fmcsdk.bean.UploadImage;
import sdk.webview.fmc.com.fmcsdk.bean.UploadVideDataBean;
import sdk.webview.fmc.com.fmcsdk.bean.UploadVideoBean;
import sdk.webview.fmc.com.fmcsdk.check.CacheHelper;
import sdk.webview.fmc.com.fmcsdk.check.bean.GetDetailsBean;
import sdk.webview.fmc.com.fmcsdk.check.bean.WorkHistoryBean;
import sdk.webview.fmc.com.fmcsdk.db.DBManager;
import sdk.webview.fmc.com.fmcsdk.db.Record;
import sdk.webview.fmc.com.fmcsdk.record.util.DeviceUtil;
import sdk.webview.fmc.com.fmcsdk.service.UploadImageService;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.DateUtil;
import sdk.webview.fmc.com.fmcsdk.util.FastClickHelper;
import sdk.webview.fmc.com.fmcsdk.util.FastClickHelper10;
import sdk.webview.fmc.com.fmcsdk.util.FileUtil;
import sdk.webview.fmc.com.fmcsdk.util.GsonUtil;
import sdk.webview.fmc.com.fmcsdk.util.ImageCompressor;
import sdk.webview.fmc.com.fmcsdk.util.ImageUtils;
import sdk.webview.fmc.com.fmcsdk.util.JpushUtils;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;
import sdk.webview.fmc.com.fmcsdk.util.SM2Utils;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;
import sdk.webview.fmc.com.fmcsdk.util.UpdateApkManager;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ApiFactory;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;
import sdk.webview.fmc.com.fmcsdk.view.GPSCustomDialog;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * Created by ydm on 2017/9/27.
 */

public class H5Presenter extends BasePresenter<H5View> {
    /**
     * 手机唯一识别码
     */
    private String imei = "";

    /**
     * 上传图片到客户端
     *
     *
     */
    public void uploadImgForClient(final String urlName, final String ids) {
        XLog.i("!!!!!!" + urlName + "----" + ids);
        File files = new File(urlName.replace("jpg", "jpeg"));

        if (!files.exists()) {
            files = new File(urlName);
            if (!files.exists()) {
                uploadCallBack(ids.split("\\.")[0], "reload123：" + ids);
                return;
            }
        }
        uploadCallBack(ids.split("\\.")[0], "reload123：" + urlName + "--" + ids);
        XLog.i(files.getPath() + "!!!!!!" + urlName + "--" + ids);
        new ImageCompressor(view.getViewContext()).compressImageInPlace(urlName, new OnCompressListener() {
            @Override
            public void onStart() {

            }
            @Override
            public void onSuccess(File file) {
                //Log.e("file1111", file.getPath());
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), ids.split("\\.")[0]);//,System.currentTimeMillis()+""

                RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

                getApi(UserApi.class).uploadImgForClient(getDefaultAddress() + Url.UPLOAD_IMG, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody,timeBody)
                        .compose(new ResponseTransformer<>(bindToLifeCycle(1)))
                        .subscribe(new ResponseSubscriber<UploadImage>(view) {
                            @Override
                            public void success(UploadImage baseData) {
                                view.uploadImgForClientSuccess(baseData, ids); //baseData.getRecord().get(0).getThumburl()
                                uploadCallBack(baseData.getRecord().get(0).getId(), "reload123：成功");
                                view.uploadImgOrVideoEnd();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                view.uploadImgForClientOnError(0, ids);
                                uploadCallBack(ids.split("\\.")[0], "reload123：" + "uploadImgForClientOnError");
                                view.uploadImgOrVideoEnd();
                            }


                            @Override
                            public void onRequestAgain() {
                                uploadImgForClient(urlName, ids);
                            }
                        });
            }

            @Override
            public void onError(Throwable e) {

            }
        });

    }

    /**
     * 图片上传到服务器
     */
    private void uploadCallBack(final String id, String remark) {
        if (BuildConfig.DEBUG) {
            getApi(UserApi.class).uploadCallBack(getDefaultAddress() + Url.UPLOAD_CALLBACK, Url.XMLHTTPREQUEST, UserHelper.getToken(), id, remark)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BaseData>() {
                        @Override
                        public void onCompleted() {
//                        ToastUtils.showShort(view.getViewContext(), "上传图片日志成功");
                        }

                        @Override
                        public void onError(Throwable e) {
                            XLog.i("baseData:" + e);
                        }

                        @Override
                        public void onNext(BaseData result) {

                        }
                    });
        }
    }


    /**
     * 图片上传成功后组装集合
     *
     * @param baseData
     * @return
     */
    public PhotoUploadBean getUploadImageSuccessList(UploadImage baseData) {
        PhotoUploadBean photoUploadBean = new PhotoUploadBean();
        //图片上传方式，2为图片上传到客户端然后将返回的参数传递给H5
        photoUploadBean.setType(Integer.parseInt(Constant.UPLOAD_IMAGE_NETWORK));
        photoUploadBean.setCode(1);
        photoUploadBean.setMsg("拍照成功");
        ArrayList<PhotoUploadBean.RecordBean> list = new ArrayList<>();
        PhotoUploadBean.RecordBean recordBean = new PhotoUploadBean.RecordBean();
        recordBean.setType(baseData.getRecord().get(0).getType());
        recordBean.setId(baseData.getRecord().get(0).getId());
        recordBean.setSuffix(baseData.getRecord().get(0).getSuffix());
        recordBean.setThumburl(baseData.getRecord().get(0).getThumburl());
        recordBean.setUrl(baseData.getRecord().get(0).getUrl());
        list.add(recordBean);
        photoUploadBean.setRecord(list);
        return photoUploadBean;
    }

    /**
     * 视频上传成功后组装集合
     *
     * @return
     */
    public UploadVideDataBean getUploadVideoSuccessList(String desc, String url, String localUrl, String id, String ownerId, String ownerObject, String ownerType) {
        UploadVideDataBean bean = new UploadVideDataBean();
        bean.setOwnerId(ownerId);
        bean.setOwnerObject(ownerObject);
        bean.setOwnerType(ownerType);
        bean.setUrl(url);
        bean.setMsg(desc);
        bean.setType(Constant.UPLOAD_VIDEO_NETWORK);
        bean.setLocalUrl(localUrl);
        bean.setId(id);
        bean.setIds(id);
        return bean;
    }

    /**
     * BASE64的方式上传图片数据
     */
    public PhotoBase64Bean getBase64SuccessList(Bundle bundle, String takePhotoType, String ownerId, String ownerObject, String ownerType, String imageName) {
        /**base64字符串*/
        String result = bundle.getString(Constant.BASE64_RESULT);
        /**文件大小*/
        long fileSize = bundle.getLong(Constant.BASE64_FILESIZE);
        /**图片的时间戳*/
        long timeStamp = bundle.getLong(Constant.TIMESTAMP);
        /**图片上传的标识*/
        int code = bundle.getInt(Constant.BASE64_CODE);
        PhotoBase64Bean bean = new PhotoBase64Bean();
        bean.setCode(String.valueOf(code));
        bean.setOwnerId(ownerId);
        bean.setOwnerObject(ownerObject);
        bean.setOwnerType(ownerType);
        if (takePhotoType.equals("1")) {
            bean.setMsg("拍照成功");
        } else {
            bean.setMsg("图片离线上传中");
        }

        bean.setType(Constant.UPLOAD_IMAGE_BASE64);
        ArrayList<PhotoBase64Bean.DataBean> list = new ArrayList<>();
        PhotoBase64Bean.DataBean dataBean = new PhotoBase64Bean.DataBean();
        dataBean.setContent(result);
        dataBean.setSize(String.valueOf(fileSize));
        dataBean.setId(String.valueOf(timeStamp));
        dataBean.setIds(imageName.split("\\.")[0]);
        list.add(dataBean);
        bean.setData(list);
        return bean;
    }


    /**
     * BASE64的方式上传图片数据
     * localpath
     */
    public PhotoBase64Bean getBase64SuccessList(Bundle bundle, String takePhotoType, String ownerId, String ownerObject, String ownerType, String imageName, String localpath) {
        /**base64字符串*/
        String result = bundle.getString(Constant.BASE64_RESULT);
        /**文件大小*/
        long fileSize = bundle.getLong(Constant.BASE64_FILESIZE);
        /**图片的时间戳*/
        long timeStamp = bundle.getLong(Constant.TIMESTAMP);
        /**图片上传的标识*/
        int code = bundle.getInt(Constant.BASE64_CODE);
        PhotoBase64Bean bean = new PhotoBase64Bean();
        bean.setCode(String.valueOf(code));
        bean.setOwnerId(ownerId);
        bean.setOwnerObject(ownerObject);
        bean.setOwnerType(ownerType);
        if (takePhotoType.equals("1")) {
            bean.setMsg("拍照成功");
        } else {
            bean.setMsg("图片离线上传中");
        }

//        ImageUtils.getH5ImageThumPath(imaname, H5Activity.this)
//        String localImageUri = FileUtil.getLocalImageUri(view.getViewContext(), localpath);
        Log.e("shuchang", "localImageUri--->" + localpath);
        bean.setType(Constant.UPLOAD_IMAGE_BASE64);
        ArrayList<PhotoBase64Bean.DataBean> list = new ArrayList<>();
        PhotoBase64Bean.DataBean dataBean = new PhotoBase64Bean.DataBean();
        dataBean.setContent(result);
        dataBean.setSize(String.valueOf(fileSize));
        dataBean.setId(String.valueOf(timeStamp));
        dataBean.setIds(imageName.split("\\.")[0]);
//        bean.setLocalImageUri(localpath);
        list.add(dataBean);
        bean.setData(list);
        return bean;
    }


    /**
     * 上传图片视频
     */
    public PhotoBase64Bean getVideoSuccessList(Bundle bundle, String takePhotoType, String ownerId, String ownerObject, String ownerType, String videoId) {
        /**图片的时间戳*/
        long timeStamp = bundle.getLong(Constant.TIMESTAMP);
        /**图片上传的标识*/
        int code = bundle.getInt(Constant.BASE64_CODE);

        PhotoBase64Bean bean = new PhotoBase64Bean();
        bean.setCode(String.valueOf(code));
        bean.setOwnerId(ownerId);
        bean.setOwnerObject(ownerObject);
        bean.setType(Constant.UPLOAD_VIDEO_SUCCESS);
        bean.setOwnerType(ownerType);
        if (takePhotoType.equals("1")) {
            bean.setMsg("录制视频成功");
        } else {
            bean.setMsg("视频离线上传中");
        }
        ArrayList<PhotoBase64Bean.DataBean> list = new ArrayList<>();
        PhotoBase64Bean.DataBean dataBean = new PhotoBase64Bean.DataBean();
        dataBean.setId(String.valueOf(timeStamp));
        dataBean.setIds(videoId);
        list.add(dataBean);
        bean.setData(list);
        return bean;
    }


    /**
     * 检查APK版本
     */
    public void checkAppVersion(final Activity activity) {
        getApi(UserApi.class).checkNewVersion(getDefaultAddress() + Url.CHECKNEWVERSION, Url.XMLHTTPREQUEST, UserHelper.getToken(), "android")
                .compose(new ResponseTransformer<>(this.<NewVervion>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<NewVervion>(view) {
                    @Override
                    public void success(NewVervion baseData) {
                        /**比较版本*/
                        compareServison(baseData, activity);
                    }

                    @Override
                    public void onRequestAgain() {
                        checkAppVersion(activity);
                    }
                });
    }

    /**
     * 比较版本号
     */
    private void compareServison(NewVervion baseData, Activity activity) {
        XLog.i(baseData.getRecord().getApkurl());
        String version = PreferenceManager.getDefaultSharedPreferences(view.getViewContext()).getString(Constant.FMC_VERSION_TIME, "");
        if (TextUtils.isEmpty(version)) {
            return;
        }
        XLog.i("baseData:" + baseData.getRecord().getVersionId());
        XLog.i("baseData:" + "DateUtil.dateToStamp(version)" + DateUtil.dateToStamp(version));
        //如果本地的时间戳小于接口返回的时间戳，说明有更新，去下载
        if (DateUtil.dateToStamp(version) < DateUtil.dateToStamp(baseData.getRecord().getVersionId())) {
            final String apkUrl = baseData.getRecord().getApkurl();
//            //自动下载
//            UpdateApkManager apkManager = new UpdateApkManager();
//            if (!apkUrl.startsWith("http")){
//                apkUrl = getDefaultAddress()+apkUrl;
//            }
//            apkManager.downloadApk(activity,apkUrl,baseData.getRecord().getVersionId());

            final GPSCustomDialog customDialog = new GPSCustomDialog(view.getViewContext(), view.getViewContext().getString(R.string.update_message));
            customDialog.show();
            customDialog.setClicklistener(new GPSCustomDialog.ClickListenerInterface() {
                @Override
                public void doConfirm() {

                    new AppUpdater(view.getViewContext(), apkUrl).start();
                    customDialog.dismiss();
                    ToastUtils.showShort(view.getViewContext(), view.getViewContext().getResources().getString(R.string.download_apk_ing));
                }

                @Override
                public void doCancel() {
//                    AppManager.getAppManager().finishAllActivity();
                    customDialog.dismiss();
                }
            });


        }
    }


    public void uploadVideo(final String localUrl, final String ids, File files) {
//        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
        RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

        RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), ids);
        getApi(UserApi.class).uploadVideo(getDefaultAddress() + Url.UPLOAD_VIDEO, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody,timeBody)
                .compose(new ResponseTransformer<>(this.<UploadVideoBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<UploadVideoBean>(view) {
                    @Override
                    public void success(UploadVideoBean baseData) {
                        view.uploadVideoSuccess(baseData.getRecord().getUrl(), localUrl, baseData.getRecord().getId());
                        view.uploadImgOrVideoEnd();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                        view.uploadVideoError(localUrl, ids);
                        view.uploadImgOrVideoEnd();
                    }


                    @Override
                    public void onRequestAgain() {

                    }
                });
    }

    /**
     * 记录当前登录日期
     */
    public void dailyLoginTime() {
        //如果未获取到手机识别码，自定义一个

        if (TextUtils.isEmpty(Utility.getIMEI(view.getViewContext()))) {
            imei = "pad";
        } else {
            imei = Utility.getIMEI(view.getViewContext());
        }
        DailyLoginTime dailyLoginTime = new DailyLoginTime(UserHelper.getUserId(),
                "1", UserHelper.getToken(), "1", imei, Build.BRAND, "android", Build.VERSION.RELEASE, "1.0", "", "");
        XLog.i(dailyLoginTime.toString());
        getApi(UserApi.class).dailyLoginTime(getDefaultAddress() + Url.DAILYLOGINTIME, UserHelper.getToken(), Url.XMLHTTPREQUEST, UserHelper.getUserId(),
                        "1", UserHelper.getToken(), "1", imei, Build.BRAND, "android", Build.VERSION.RELEASE, "1.0", "true", "")
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>() {
                    @Override
                    public void success(BaseData baseData) {
                        String currentDate = DateUtil.getCurrentDate();
                        XLog.i("当前时间：" + currentDate);
                        getSharedPrefs().edit().putString(Constant.LOGIN_TIME, DateUtil.getCurrentDate()).apply();

                    }

                    @Override
                    public void onRequestAgain() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


    /**
     * 修改密码
     *
     * @param initialPassword 旧密码
     * @param newPassword     新密码
     * @param againPassword   确认新密码
     */
    public void modifyPassword(final String initialPassword, final String newPassword, final String againPassword) {
        view.showLoading();
        getApi(UserApi.class).modifyPassword(getDefaultAddress() + Url.MODIFY_PASSWORD, Url.XMLHTTPREQUEST, UserHelper.getToken(), initialPassword, newPassword)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.modifyPasswordSuccess();
                        view.ToastMessage(R.string.change_success);
                    }

                    @Override
                    public void onRequestAgain() {
                        modifyPassword(initialPassword, newPassword, againPassword);
                    }
                });
    }


    /**
     * 获取初始密码修改
     */
    public void getpwdEnforced(final String accessToken, String userid) {
        getApi(UserApi.class).getpwdEnforced(getDefaultAddress() + Url.GET_PSD_ENFORCED, Url.XMLHTTPREQUEST, accessToken, Long.parseLong(userid))
                .compose(new ResponseTransformer<>(this.<ModfiyPassword>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<ModfiyPassword>() {
                    @Override
                    public void success(ModfiyPassword baseData) {
                        XLog.i("getpwdEnforced--" + baseData.getRecord().getPwdEnforced());

                        //强制修改密码
                        view.getpwdEnforcedSuccess(baseData.getRecord().getPwdEnforced());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {

                    }

                });
    }


    /**
     * 获取设备id
     */
    public void updateDeviceId() {
        getApi(UserApi.class).updateDeviceId(getDefaultAddress() + Url.UPDATE_DEVICEID, Url.XMLHTTPREQUEST, UserHelper.getToken(), Utility.getIMEI(view.getViewContext()))
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>() {
                    @Override
                    public void success(BaseData baseData) {
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {
                    }
                });
    }


    /**
     * 获取别名配置,兼容别的项目，所以这里成功和失败都要开启，有的项目没有这个接口
     */
    public void getJpushalias() {

        getApi(CommonApi.class).getJpushalias(getDefaultAddress() + Url.JPUSHFLAGOFENV)
                .compose(new ResponseTransformer<>(this.<JpushAliasBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<JpushAliasBean>() {
                    @Override
                    public void success(JpushAliasBean baseData) {
                        XLog.i("-----"+baseData.getMessage());
                        PreferencesHelper.saveData(Constant.JPUSH_ALIAS, baseData.getMessage());
                        //设置极光
                        new JpushUtils().startPush(view.getViewContext());
                    }

                    @Override
                    public void onRequestAgain() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        //设置极光
                        new JpushUtils().startPush(view.getViewContext());
                    }
                });
    }

    public void getSM2Key(final String initialPassword, final String newPassword, String againPassword) {
        view.showLoading();
        getApi(UserApi.class).getSM2Key(getDefaultAddress() + Url.GET_SM2KEY, Url.XMLHTTPREQUEST)
                .compose(new ResponseTransformer<>(this.<ModfiyPassword>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<ModfiyPassword>() {
                    @Override
                    public void success(ModfiyPassword baseData) {
                        modifyPasswordChar(baseData.getRecord().getPublicKey(), initialPassword, newPassword);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {
                    }
                });
    }

    /**
     * 修改密码
     *
     * @param initialPassword 旧密码
     * @param newPassword     新密码
     */
    public void modifyPasswordChar(final String key, final String initialPassword, final String newPassword) {
        view.showLoading();
        getApi(UserApi.class).modifyPasswordChar(getDefaultAddress() + Url.MODIFY_PASSWORD, Url.XMLHTTPREQUEST, UserHelper.getToken(), key, SM2Utils.encode(key, initialPassword), SM2Utils.encode(key, newPassword))
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.modifyPasswordSuccess();
                        view.ToastMessage(R.string.change_success);
                    }

                    @Override
                    public void onRequestAgain() {
                        modifyPasswordChar(key, SM2Utils.encode(key, initialPassword), SM2Utils.encode(key, newPassword));
                    }
                });
    }


    public void getDetails(String id) {
        getApi(UserApi.class).apiGetDetail(getDefaultAddress() + Url.GET_DETAILS, UserHelper.getToken(), id)
                .compose(new ResponseTransformer<>(this.<GetDetailsBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<GetDetailsBean>(view) {
                    @Override
                    public void success(GetDetailsBean baseData) {
                        view.getDetailSuccess(baseData);
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.getDetailFail();
                        view.hideLoading();
                    }

                    @Override
                    public void onRequestAgain() {

                    }
                });
    }

    public void getResult() {
        view.showLoading();
        getApi(UserApi.class).getLangList(getDefaultAddress() + Url.GET_DD_LIST, Url.XMLHTTPREQUEST, UserHelper.getToken(), "SUSPEND_REMARK")
                .compose(new ResponseTransformer<>(this.<DDTypeBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<DDTypeBean>() {
                    @Override
                    public void success(DDTypeBean baseData) {
                        view.getResult(baseData);
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.hideLoading();
                    }

                    @Override
                    public void onRequestAgain() {
                    }


                });


    }
    public void getOperateReportList() {
        view.showLoading();
        getApi(UserApi.class).getOperateReportList(getDefaultAddress() + Url.GET_OPERATE_REPORTSAS_LIST,  UserHelper.getToken(), "2")
                .compose(new ResponseTransformer<>(this.<OperateReportBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<OperateReportBean>() {
                    @Override
                    public void success(OperateReportBean baseData) {
                        if (null != baseData){
                            if (null != baseData.getRecords() && !baseData.getRecords().isEmpty()){
                                CacheHelper.getInstance().addMapJson("ReportList", GsonUtil.BeanToJson(baseData.getRecords()));
                                FMCApplication.shutDownList.clear();
                                FMCApplication.shutDownList.addAll(baseData.getRecords());
                            }
                        }
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.hideLoading();
                    }

                    @Override
                    public void onRequestAgain() {
                    }


                });


    }


    public void getDetailsHistory(String id,String projId) {
        getApi(UserApi.class).apiGetHistory(getDefaultAddress() + Url.GET_HISTORY, UserHelper.getToken(), Long.valueOf(id),projId)
                .compose(new ResponseTransformer<>(this.<WorkHistoryBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<WorkHistoryBean>(view) {
                    @Override
                    public void success(WorkHistoryBean baseData) {
                        view.getHistory(baseData,id,projId);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                    }

                    @Override
                    public void onRequestAgain() {

                    }
                });
    }
}
